<!--
  module - 企业管理

  @author lgx 2021-06-07
-->
<template>
  <div class="mixin-components-container">
    <el-col :span="24">
      <el-card>
        <div slot="header" class="clearfix">
          <template>
            <el-page-header :content="titleContent" @back="goBack" />
          </template>
        </div>
        <div>
          <div>
            <div style="margin-bottom:50px;">
              <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="140px"
                style="padding: 25px 0;"
                :disabled="$route.query.type === '3' ? true : false"
              >
                <el-row :gutter="20">
                  <el-col :span="10">
                    <el-form-item prop="name" label="企业名称">
                      <el-input v-model="form.name" placeholder="请填写企业名称" maxlength="50" clearable />
                    </el-form-item>
                    <el-form-item prop="address" label="企业地址">
                      <el-input v-model="form.address" type="string" placeholder="企业地址" />
                    </el-form-item>
                    <el-form-item prop="userIdList" label="管理员">
                      <el-select v-model="form.userIdList" multiple filterable placeholder="请选择管理员">
                        <el-option
                          v-for="item in options"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="10">
                    <el-form-item prop="contact" label="联系人">
                      <el-input v-model="form.contact" type="code" placeholder="联系人" />
                    </el-form-item>
                    <el-form-item prop="phone" label="联系人电话">
                      <el-input
                        v-model="form.phone"
                        placeholder="请填写联系人电话"
                        maxlength="20"
                        show-word-limit
                        clearable
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
              <el-row style="padding-left:140px;">
                <!-- <el-button
                  v-if="$route.query.type === '1' || $route.query.type === '2'"
                  waves
                  type="primary"
                  @click="submitForm('form')"
                >
                  保存
                </el-button> -->
                <el-link v-if="$route.query.type === '1' || $route.query.type === '2'" type="primary" size="mini" @click="submitForm('form')">
                  保存
                </el-link>
                <!-- <el-button waves type="info" @click="goBack">取消</el-button> -->
                <el-link type="info" size="mini" @click="goBack">
                  取消
                </el-link>
              </el-row>
            </div>
          </div>
        </div>

      </el-card>
    </el-col>
  </div>
</template>

<script>
// api
import { saveAndUpdate, queryData } from '@/api/enterprise/sysEnterprise'
import { queryUserList } from '@/api/userAndRole/sysUser'

// 水波纹插件
import waves from '@/directive/waves'

export default {
  name: 'SaveAndEditSysEnterprise',
  directives: { waves },
  data() {
    return {
      listLoading: false,
      titleContent: '',
      options: [],
      form: {
        enterpriseId: '',
        name: '',
        address: '',
        contact: '',
        phone: '',
        userIdList: '',
        status: false
      },
      rules: {
        name: [
          { required: true, message: '请填写企业名称', trigger: 'blur' }
        ]
      },
      value: []
    }
  },
  created() {
    const curVue = this
    // 设置部门选项
    if (curVue.$route.query.type === '1') {
      curVue.titleContent = '新建企业'
    } else {
      curVue.form.enterpriseId = curVue.$route.query.data
      if (curVue.$route.query.type === '2') {
        curVue.titleContent = '编辑企业'
      } else if (curVue.$route.query.type === '3') {
        curVue.titleContent = '查看企业'
      }
    }
    this.queryUser()
    if (!curVue.form.enterpriseId) {
      return
    }
    queryData({ enterpriseId: curVue.form.enterpriseId }).then(res => {
      if (res.code === 1) {
        this.form = res.data
      } else {
        this.noticeUtil.errorMsg('操作失败!')
      }
    })
  },
  methods: {
    queryUser() {
      const curVue = this
      const query = {
        limit: 0,
        page: 0
      }
      queryUserList(query).then(res => {
        if (res.code === 1) {
          console.log(res.data.userList)
          for (const user of res.data.userList) {
            if (user.userName === 'admin') {
              continue
            }
            curVue.options.push({ value: user.userId, label: user.realName })
          }
        } else {
          this.noticeUtil.errorMsg('用户数据获取失败!')
        }
      })
    },
    goBack() {
      this.$router.go(-1)
    },
    handleAvatarSuccess(res, file) {
      this.head = URL.createObjectURL(file.raw)
    },
    submitForm(formName) {
      const formObj = {
        enterpriseId: this.form.enterpriseId,
        name: this.form.name,
        address: this.form.address,
        contact: this.form.contact,
        phone: this.form.phone,
        userIdList: this.form.userIdList
      }
      this.$refs[formName].validate((valid) => {
        saveAndUpdate(formObj).then(res => {
          if (res.code === 1) {
            this.noticeUtil.successMsg('操作成功!')
            this.$router.go(-1)
          } else {
            this.noticeUtil.errorMsg('操作失败!')
          }
        })
      })
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 148px;
    height: 148px;
    line-height: 148px;
    text-align: center;
  }
  .avatar {
    width: 148px;
    height: 148px;
    display: block;
  }
</style>
